<template>
  <div
    v-loading="pageLoading"
    :element-loading-text="loadingOption.text"
    :element-loading-spinner="loadingOption.spinner"
    :element-loading-background="loadingOption.background"
    class="orderDetail_main"
  >
    <el-card shadow="never" class="card">
      <!-- 订单信息 -->
      <div class="table-main">
        <div class="table-title">
          订单信息:
          <span style="margin-left: 10px">
            <el-tag
              v-if="status === 0"
              type="danger"
              size="small"
              effect="dark"
            >
              {{ statusList[status].name }}
            </el-tag>
            <el-tag
              v-else-if="status === 1"
              size="small"
              type="warning"
              effect="dark"
            >
              {{ statusList[status].name }}
            </el-tag>
            <el-tag v-else-if="status === 2" size="small">
              {{ statusList[status].name }}
            </el-tag>
            <el-tag v-else-if="status === 3" size="small" type="info">
              {{ statusList[status].name }}
            </el-tag>
          </span>
        </div>
        <!-- <TableComponent
          :table-data="orderTableData"
          :table-style="{
            width: '100%',
            margin: '30px auto',
            fontSize: '16px'
          }"
        >
          <template slot="btn">
            <el-button
              type="primary"
              size="mini"
              style="margin-left: 10px"
              @click="goOrderDetail"
            >查看信息</el-button>
          </template>
        </TableComponent> -->
        <OrderTable :table-data="orderTableData" />
        <div v-if="imgList.length > 0" class="img_main">
          <div class="left">退单图片:</div>
          <div class="right">
            <el-image
              v-for="(item, index) in imgList"
              :key="index"
              style="width: 100px; height: 100px; margin-right: 20px"
              :src="item"
              :preview-src-list="imgList"
            />
          </div>
        </div>
      </div>
      <el-divider />

      <!-- 退货商品 -->
      <div class="table-main">
        <div class="table-title">退货商品:</div>
        <el-table :data="productList" border style="width: 100%">
          <!-- 商品列表内容 -->
          <el-table-column label="商品图片" width="250">
            <template slot-scope="scope">
              <!-- <router-link></router-link> -->
              <img :src="scope.row.productPic" class="table_img">
            </template>
          </el-table-column>
          <el-table-column align="center" label="商品描述">
            <template slot-scope="scope">
              <p>{{ scope.row.productName }}</p>
              <p>品牌: {{ scope.row.productBrand }}</p>
            </template>
          </el-table-column>

          <el-table-column align="center" label="商品价格" width="250">
            <template slot-scope="scope">
              <span>{{ scope.row.productRealPrice | money }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="productAttr"
            label="商品属性"
            width="250"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.productAttr | initAttrArr }}</p>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="productCount"
            label="商品数量"
            width="250"
          />

          <el-table-column align="center" prop="returnAmount" label="总计">
            <template slot-scope="scope">
              <span>{{ scope.row.returnAmount }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 用户信息 -->
      <div class="table-main">
        <div class="table-title">用户信息:</div>
        <!-- <TableComponent
          :table-data="userTableData"
          :table-style="{
            width: '100%',

            fontSize: '16px'
          }"
        /> -->
        <OrderTable :table-data="userTableData" />
      </div>
      <el-divider />

      <!-- 费用信息 -->
      <div class="table-main">
        <div class="table-title">费用信息:</div>
        <!-- <TableComponent
          :table-data="costData"
          :table-style="{
            width: '100%',

            fontSize: '16px'
          }"
        /> -->
        <OrderTable :table-data="costData" />

        <div
          v-if="status === 0"
          :style="{
            width: '100%',
            margin: '10px auto',
            fontSize: '16px'
          }"
        >
          <!-- 处理是否退货信息的表单 -->
          <el-form
            ref="handleForm"
            :model="handleForm"
            :rules="handleRules"
            size="mini"
            class="form-lable"
          >
            <el-row :gutter="20">
              <el-col :span="6" :offset="0">
                <el-form-item prop="returnAmount" label="退款金额">
                  <el-input-number
                    v-model="handleForm.returnAmount"
                    class="myInput"
                    placeholder=""
                    size="mini"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="0">
                <el-form-item prop="handleMan" label="处理人">
                  <el-input
                    v-model="handleForm.handleMan"
                    class="myInput"
                    type="text"
                    placeholder=""
                    size="mini"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="0">
                <el-form-item prop="handleNote" label="处理备注">
                  <el-input
                    v-model="handleForm.handleNote"
                    class="myInput"
                    type="text"
                    placeholder=""
                    size="mini"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <el-divider v-if="status > 0" />

      <!-- 处理结果信息 -->
      <div v-if="status !== 0" class="table-main">
        <div class="table-title">处理结果:</div>
        <!-- <TableComponent
          :table-data="resultData"
          :table-style="{
            width: '100%',
            margin: '30px auto',
            fontSize: '16px'
          }"
        /> -->
        <OrderTable :table-data="resultData" />
      </div>

      <div
        v-if="status === 1"
        :style="{
          width: '100%',
          margin: '10px auto',
          fontSize: '16px'
        }"
      >
        <el-form
          ref="receiveForm"
          :model="receiveForm"
          :rules="receiveRules"
          size="mini"
          class="form-lable"
        >
          <el-row :gutter="20">
            <el-col :span="6" :offset="0">
              <el-form-item label="收货人" prop="receiveMan">
                <el-input
                  v-model="receiveForm.receiveMan"
                  class="myInput myInput2"
                  type="text"
                  placeholder=""
                  size="mini"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6" :offset="0">
              <el-form-item label="收货备注" prop="receiveNote">
                <el-input
                  v-model="receiveForm.receiveNote"
                  class="myInput myInput2"
                  type="text"
                  placeholder=""
                  size="mini"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-divider v-if="status > 1" />

      <div v-if="status === 2" class="table-main">
        <div class="table-title">收货信息:</div>
        <!-- <TableComponent

          :table-data="receiveData"
          :table-style="{
            width: '100%',
            margin: '30px auto',
            fontSize: '16px'
          }"
        /> -->
        <OrderTable :table-data="receiveData" />
      </div>
      <div class="button_main">
        <!-- v-if="step===1" -->
        <el-button
          v-if="status === 0"
          type="primary"
          style="width: 150px"
          size="small"
          @click="agreeReturn"
        >同意退款
        </el-button>
        <el-button
          v-if="status === 1"
          type="primary"
          style="width: 150px"
          size="small"
          @click="receiveReturn"
        >确认收货
        </el-button>
        <el-button
          v-if="status === 0"
          style="width: 150px"
          size="small"
          @click="rejectReturn"
        >拒绝退款
        </el-button>
        <!-- <el-button type="primary" size="default" ></el-button> -->
      </div>
    </el-card>
  </div>
</template>

<script>
import orderApi from '@/api/order/index'
// import tableComponent from '@/components/tableComponent/index'
import mixin from '@/mixins/index'
export default {
    name: 'OrderBackDetail',
    // components: { tableComponent },
    mixins: [mixin],
    data() {
        return {
            pageLoading: false,
            id: '', // id
            status: -1, // 订单状态
            detailObj: {}, // 接口获取到的详情对象
            orderTableData: [], //
            userTableData: [], //
            costData: [], //
            resultData: [], //
            receiveData: [], //
            productList: [], // 商品数组
            imgList: [],
            statusList: [
                { name: '待处理', id: 0 },
                { name: '退货中', id: 1 },
                { name: '已完成', id: 2 },
                { name: '已拒绝', id: 3 }
            ],
            // 秒杀类型列表
            handleForm: {
                returnAmount: 0, // 退款金额
                handleNote: '', // 退款备注
                handleMan: '' // 处理人
            },
            receiveForm: {
                receiveNote: '', // 收穫備注
                receiveMan: '' // 收货人
            },
            handleRules: {
                handleMan: [{ required: true, message: '处理人姓名', trigger: 'blur' }],
                handleNote: [{ required: true, message: '处理人备注', trigger: 'blur' }]
            },
            receiveRules: {
                receiveMan: [
                    { required: true, message: '收货人姓名', trigger: 'blur' }
                ],
                receiveNote: [
                    { required: true, message: '收货人备注', trigger: 'blur' }
                ]
            }

            // 拒绝或者接受备注
        }
    },
    computed: {},
    created() {
        this.id = this.$route.params.id
        this.getDetail()
    },
    methods: {
        getDetail() {
            this.pageLoading = true
            orderApi.orderBackDetail(this.id).then((res) => {
                if (res.success) {
                    this.detailObj = res.data.orderReturnApply
                    this.status = this.detailObj.status
                    // 初始化退货照片数组
                    this.initImgs()
                    // 初始化商品信息
                    this.initProductTable()
                    // 初始化 订单信息
                    this.initOrderObj()
                    // 初始化 用户信息
                    this.initUserObj()
                    // 初始化 费用信息
                    this.initCostObj()
                    // 初始化 处理结果信息
                    this.initResultObj()
                    // 初始化收货信息
                    this.initReceiveObj()
                }
                this.pageLoading = false
            })
        },
        // 初始化商品信息
        initProductTable() {
            const obj = {
                productPic: this.detailObj.productPic, // 商品图片
                productName: this.detailObj.productName, // 商品名字
                productAttr: this.detailObj.productAttr, // 属性
                productCount: this.detailObj.productCount, // 数量
                productBrand: this.detailObj.productBrand, // 品牌
                productRealPrice: this.detailObj.productRealPrice, // 价格
                returnAmount: this.detailObj.returnAmount // 总计
            }
            this.productList.push(obj)
        },
        initImgs() {
            if (this.detailObj.proofPics) {
                this.imgList = this.detailObj.proofPics.split(',')
            }
        },
        initReceiveObj() {
            const receiveObj = {
                退单收货人: this.detailObj.receiveMan,
                退单收货备注: this.detailObj.receiveNote,
                退单收货时间: this.detailObj.receiveTime
            }
            // orderTableData
            for (const key in receiveObj) {
                this.receiveData.push({
                    key: key,
                    value: receiveObj[key]
                })
            }
        },
        // 初始化订单信息
        initOrderObj() {
            const orderObj = {
                // 当前状态: this.statusList[this.detailObj.status].name, // 当前状态
                订单编号: this.detailObj.orderSn,
                退单人姓名: this.detailObj.returnName, // 当前状态
                退单理由: this.detailObj.reason, // 当前状态
                退单描述: this.detailObj.description, // 当前状态
                申请时间: this.detailObj.createTime // 当前状态
            }
            // orderTableData
            for (const key in orderObj) {
                this.orderTableData.push({
                    key: key,
                    value: orderObj[key],
                    orderId: key === '订单编号' ? this.detailObj.orderId : ''
                })
            }
        },
        // 初始化用户信息 补全 问题
        initUserObj() {
            const userObj = {
                联系人: this.detailObj.returnPhone,
                联系电话: this.detailObj.returnPhone
            }
            for (const key in userObj) {
                this.userTableData.push({
                    key: key,
                    value: userObj[key]
                })
            }
        },
        // 初始化用户信息 补全 问题
        initCostObj() {
            const costObj = {
                退单金额: this.detailObj.returnAmount,
                运费金额: 0
            }

            for (const key in costObj) {
                this.costData.push({
                    key: key,
                    value: costObj[key]
                })
            }
        },
        initResultObj() {
            const resultObj = {
                处理人: this.detailObj.handleMan,
                处理备注: this.detailObj.handleNote,
                处理时间: this.detailObj.handleTime
            }

            for (const key in resultObj) {
                this.resultData.push({
                    key: key,
                    value: resultObj[key]
                })
            }
        },
        // 同意退款
        agreeReturn() {
            this.$refs.handleForm.validate((valid) => {
                if (valid) {
                    orderApi
                        .agreeReturn(this.id, {
                            id: this.id,
                            handleMan: this.handleForm.handleMan,
                            handleNote: this.handleForm.handleNote,
                            returnAmount: this.handleForm.returnAmount
                        })
                        .then((res) => {
                            if (res.success) {
                                this.$message.success('成功')
                                // window.location.reload()
                                this.getDetail()
                            } else {
                                this.$message.error('请求失败')
                            }
                        })
                } else {
                    this.$message.warning('请填写必要信息')
                }
            })
        },
        // 确认收货
        receiveReturn() {
            this.$refs.receiveForm.validate((valid) => {
                if (valid) {
                    orderApi
                        .receiveReturn(this.id, {
                            id: this.id,
                            receiveMan: this.receiveForm.receiveMan,
                            receiveNote: this.receiveForm.receiveNote
                        })
                        .then((res) => {
                            if (res.success) {
                                this.$message.success('成功')
                                window.location.reload()
                            } else {
                                this.$message.error('请求失败')
                            }
                        })
                } else {
                    this.$message.warning('请填写必要信息')
                }
            })
        },
        // 拒绝退款
        rejectReturn() {
            this.$refs.handleForm.validate((valid) => {
                if (valid) {
                    orderApi
                        .rejectReturn(this.id, {
                            id: this.id,
                            handleMan: this.handleForm.handleMan,
                            handleNote: this.handleForm.handleNote
                        })
                        .then((res) => {
                            if (res.success) {
                                this.$message.success('成功')
                                window.location.reload()
                            } else {
                                this.$message.error('请求失败')
                            }
                        })
                } else {
                    this.$message.warning('请填写必要信息')
                }
            })
        },
        // 跳转查看订单详情页面
        goOrderDetail() {
            this.$router.push({
                name: 'orderDetail',
                params: { id: this.detailObj.orderId }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.orderDetail_main {
  min-height: 900px;
  .form-lable {
    ::v-deep .el-form-item {
      label {
        font-weight: normal;
      }
    }
  }
  .flexCenter {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .card {
    margin: 30px;
  }
  .table-main {
    margin: 20px 0;
    .table-title {
      color: rgba(0, 0, 0, 0.85);
      font-weight: 700;
      font-size: 16px;
      margin-bottom: 20px;
    }
  }
  .button_main {
    margin: 40px auto;
    display: flex;
    width: 100%;
    justify-content: center;
  }
  .table_img {
    width: 100px;
    height: 100px;
  }
  .myInput {
    width: 70%;
    margin-left: 10px;
  }
  // .myInput2 {
  //   width: 675px;
  //   margin-right: 100px;
  // }
  .img_main {
    width: 100%;
    overflow: hidden;
    font-size: 14px;
    margin-bottom: 20px;
    .left {
      float: left;
      width: 5%;
    }
    .right {
      position: relative;
      overflow: hidden;
      float: left;
      width: 90%;
      margin-left: 20px;
      img {
        float: left;
        width: 150px;
        height: 150px;
        margin: 10px;
      }
    }
  }
}
</style>
